<?php
/**
 * Created by PhpStorm.
 * User: lih
 * Date: 2018/4/22
 * Time: 10:19
 */

namespace common\components;

use Yii;

class PostMan
{

    public function getApiVersion()
    {
        return "100";
    }

    /**
     * @param $url string
     * @return string
     */
    public function httpGet($url)
    {
        return $this->httpGetWithStream($url);
    }

    /**
     * @param $url string
     * @return string
     */
    public function httpGetWithStream($url)
    {
        $respData = '';
        try {
            $opts = array(
                'http'=>array(
                    'method'=>"GET",
                    'timeout'=>3,
                )
            );
            $context = stream_context_create($opts);
            $returnData = file_get_contents($url, false, $context);
            if(is_string($returnData)) {
                $respData = $returnData;
            }
        } catch(\Exception $e) {
        }
        return $respData;
    }

    /**
     * @param $url string
     * @param $data string form data use http_build_query.
     * @return string
     */
    public function httpPostWithStream($url, $data) {
        $respData = '';
        try {
            $opts = array(
                'http'=>array(
                    'method'=>"POST",
                    "header"=>"Content-type:application/x-www-form-urlencoded",
                    'content' => $data,
                    'timeout'=>3,
                )
            );
            $context = stream_context_create($opts);
            $returnData = file_get_contents($url, false, $context);
            if(is_string($returnData)) {
                $respData = $returnData;
            }
        } catch(\Exception $e) {
        }
        return $respData;
    }

}